<?php

/**
 * Framework_Navigator
 *
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @copyright   (c) 2007 Ian Szewczyk. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package Framework
 * @filesource
 */

/**
 * Framework_Navigator
 *
 *
 * @author Ian Szewczyk <roamzero@gmail.com>
 * @package Framework
 * @todo Make it work with reference arguments
 */
class Framework_Navigator 
{
	/**
	 * $baseUrl
	 *
	 * @access      private
	 * @var         string      $baseUrl
	 */
	private $baseUrl;
	
	/**
	 * $cssClass
	 *
	 * @access      private
	 * @var         string      $cssClass
	 */
	private $cssClass;
	
	/**
	 * $cssId
	 *
	 * @access      private
	 * @var         string      $cssId
	 */
	private $cssId;
	
	/**
	 * $navItems
	 *
	 * @access      private
	 * @var         array     $navItems
	 */
	private $navItems;
	
	/**
	 * $sortArray
	 *
	 * @access      private
	 * @var         array     $sortArray
	 */
	private  $sortArray;
	
	/**
     * __construct
     *
	 * @access 		public
     * @param		string		$baseUrl
     * @param		string		id
     * @param		string		$class	
     * @return 		void
     */
	public function __construct($baseUrl, $id, $class = null)
 	{
 		$this->baseUrl = Framework::$request->getBaseHTTPPath().$baseUrl;
 		$this->cssClass = $class;
 		$this->cssId = $id;
 	}
 	
 	/**
     * getNavigation
     *
	 * @access 		public
     * @param		string		$type
     * @return 		string		HTML formatted navigator
     */
 	public function getNavigation($type = 'ul')
 	{
 		$navHTML = '<'.$type.' id="'.$this->cssId.'"'.($this->cssClass ? ' class="'.$this->cssClass.'"' : '').'>';
 		foreach ($this->navItems as $id => $navItem) {
 			$navHTML .= '<li'.( $navItem['active'] ? ' class="active"' : '').'><a href="'.$this->baseUrl.$navItem['urlAppend'].'" id="'.$id.'"'.($navItem['onClick'] ? ' onClick="'.$navItem['onClick'].'"' : '').( $navItem['class'] ? ' class="'.$navItem['class'].'"' :'').'><span>'.$navItem['displayLink'].'</span></a></li>';
 		}
 		$navHTML .= '</'.$type.'>';
 		
 		return $navHTML;
 	}
 	
 	
 	/**
     * getNavigation
     *
	 * @access 		public
     * @param		string		$type
     * @return 		string		HTML formatted navigator
     */
 	public function getNavItems()
 	{
 		return $this->navItems;
 	}
 	
 	/**
     * setItem
     *
	 * @access 		public
     * @param		string		$id
     * @param		string		$urlAppend
     * @param		string		$displayLink
     * @param		string		$onClick 
     * @param		string		$active
     * @param		string		$class
     * @return 		void
     */
 	public function setItem($id, $urlAppend = null, $displayLink = null, $onClick = null, $active = false, $class = null)
 	{
 		if(!$displayLink) $displayLink = $id;
 		if(!$urlAppend) $urlAppend = '';
 		$this->navItems[$id] = array('urlAppend' => $urlAppend, 'displayLink' => $displayLink,'onClick' => $onClick, 'active' => $active, 'class' => $class);
 	}
 	
 	/**
     * setOrder
     *
	 * @access 		public
     * @param		array	$sortArray
     * @return 		void
     */
 	public function setOrder($sortArray) 
 	{
 		$this->sortArray = $sortArray;
 		uksort($this->navItems,array($this,'cmp'));
 	}
 	
 	/**
     * cmp
     *
	 * @access 		public
     * @param		mixed		$a
     * @param		mixed		$b
     * @return 		integer
     */
	//the custom compare function
	public function cmp($a,$b){
  
  		$PosA = $this->keyPos($a,$this->sortArray);
  		$PosB = $this->keyPos($b,$this->sortArray);
  		if ($PosA == $PosB){return 0;} else {return ($PosA > $PosB ? 1 : -1);}
	}

	
	/**
     * keyPos
     *
	 * @access 		public
     * @param		mixed		$key
     * @param		array		$sortArray
     * @return 		integer
     */
	//where is my key in my array
	private function keyPos($key,&$sortArray){
   		$i=0;
   		$pos=99999999;
   		if($sortArray){
    		 foreach($sortArray as $k){
         		$i++;
         		if($k == $key){
           			$pos=$i;
           			break;
         		}		
     		}
   		}
   		return $pos;
	}
 	
	/**
     * setActive
     *
	 * @access 		public
     * @param		string		$id
     * @return 		void
     */
 	public function setActive($id)
 	{
 		foreach ($this->navItems as $navKey => $navItem) {
 			if ($navKey == $id) 
 				$this->navItems[$navKey]['active'] = true;
 			else 
 				$this->navItems[$navKey]['active'] = false;
 		}
 			
 	}
 }

?>